﻿using UnityEngine;
using System.Collections;

public class RandomizeSpawns : MonoBehaviour 
{
	Vector3[] spawns = {new Vector3(-80f, .5f, 80f), new Vector3(-80f, .5f, -80f), new Vector3(80f, .5f, -80f), new Vector3(80f, .5f, 80f)};
	private GameObject playerBase;
	private GameObject enemyBase;
	public Player neutral;
	public EnemyAI aiTest;
	private GameObject incomeTower1;
	private GameObject incomeTower2;
	
	// Use this for initialization
	void Start () 
	{
		//Determine random starting locations
		int player = Random.Range (0, 3);
		int enemy = Random.Range (0, 3);
		while(enemy == player)
			enemy = Random.Range (0, 3);
		
		//Move the player and enemy bases to those locations
		playerBase = GameObject.Find ("Player");
		playerBase.transform.position = spawns[player];
		Player.Controller=((Player)playerBase.GetComponent("Player"));
		((Player)playerBase.GetComponent("Player")).commandCenter.obj.renderer.material.color= ((Player)playerBase.GetComponent("Player")).myColor;
		enemyBase = GameObject.Find ("AI");
		enemyBase.transform.position = spawns[enemy];
		((Player)enemyBase.GetComponent("Player")).commandCenter.obj.renderer.material.color= ((Player)enemyBase.GetComponent("Player")).myColor;

		//Determine income tower locations
		int income1 = 0;
		while(income1 == player || income1 == enemy)
			income1++;
		int income2 = 0;
		while(income2 == player || income2 == enemy || income2 == income1)
			income2++;

		//Place the income towers
		incomeTower1 = GameObject.Find ("incometower_001");
		incomeTower2 = GameObject.Find ("incometower_002");
		foreach(Renderer o in incomeTower1.GetComponentsInChildren<Renderer>()){
			o.material.color=neutral.myColor;
		}
		incomeTower1.renderer.material.color = incomeTower2.renderer.material.color;
		incomeTower1.transform.position = spawns[income1];
		foreach(Renderer o in incomeTower2.GetComponentsInChildren<Renderer>()){
			o.material.color=neutral.myColor;
		}
		incomeTower2.renderer.material.color = incomeTower1.renderer.material.color;
		incomeTower2.transform.position = spawns[income2];
		if(aiTest!=null){
			aiTest.it1Loc=income1;
			aiTest.it2Loc=income2;
			aiTest.enemyLoc=player;
			aiTest.myLoc=enemy;
		}
	}
}
